package main

import (
	"log"

	"dormitory/database"
	"dormitory/handlers"

	"github.com/gin-gonic/gin"
)

func main() {
	// 初始化数据库
	database.InitDB()

	// 创建Gin引擎
	r := gin.Default()

	// 加载HTML模板
	r.LoadHTMLGlob("templates/*")

	// 设置静态文件目录
	r.Static("/static", "./static")

	// 页面路由
	r.GET("/", func(c *gin.Context) {
		c.HTML(200, "index.html", nil)
	})
	r.GET("/dormitory", func(c *gin.Context) {
		c.HTML(200, "dormitory.html", nil)
	})
	r.GET("/repair", func(c *gin.Context) {
		c.HTML(200, "repair.html", nil)
	})

	// API路由组
	api := r.Group("/api")
	{
		// 学生相关路由
		api.GET("/students", handlers.GetStudents)
		api.POST("/students", handlers.CreateStudent)
		api.GET("/students/:id", handlers.GetStudent)
		api.PUT("/students/:id", handlers.UpdateStudent)
		api.DELETE("/students/:id", handlers.DeleteStudent)

		// 宿舍相关路由
		api.GET("/dormitories", handlers.GetDormitories)
		api.POST("/dormitories", handlers.CreateDormitory)
		api.GET("/dormitories/:id", handlers.GetDormitory)
		api.PUT("/dormitories/:id", handlers.UpdateDormitory)
		api.DELETE("/dormitories/:id", handlers.DeleteDormitory)

		// 维修申请相关路由
		api.GET("/repairs", handlers.GetRepairRequests)
		api.POST("/repairs", handlers.CreateRepairRequest)
		api.GET("/repairs/:id", handlers.GetRepairRequest)
		api.PUT("/repairs/:id", handlers.UpdateRepairRequest)
		api.DELETE("/repairs/:id", handlers.DeleteRepairRequest)
	}

	// 启动服务器
	log.Println("Server is running on http://localhost:8080")
	if err := r.Run(":8080"); err != nil {
		log.Fatal("Failed to start server:", err)
	}
}
